// https://leetcode.cn/problems/longest-consecutive-sequence/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> numSet;
        for (int num : nums) {
            numSet.insert(num);
        }

        int maxLen = 0;
        for (int num : numSet) {
            if (numSet.count(num - 1)) {
                continue;
            }

            int cur = num;
            int len = 1;
            while (numSet.count(cur + 1)) {
                len++;
                cur++;
            }
            maxLen = max(maxLen, len);
        }
        return maxLen;
    }
};